<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/30
 * @Description: 意见反馈表
 */

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class ImFeedback extends BaseModel
{
    protected $table = 'im_feedback';

    //反馈用户关联
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'user_id', 'id')
            ->select(['id', 'nickname', 'headimg']);
    }

    //处理进度
    public function process()
    {
        return $this->hasMany(ImFeedbackProcess::class, 'feedback_id', 'id');
    }

    /**
     * 获取图片字段格式转换
     * @param $value
     * @return mixed
     * @author an
     * @time 2021-12-14 14:50
     */
    public function getPictureAttribute($value)
    {
        return json_decode($value, true);
    }

    /**
     * @desc 添加意见反馈
     * @param array $input 数组
     * @param int $userId
     * @return array
     * @author zhouzhou
     * @time 2021/12/3 12:45
     */
    public function addFeedback($input, $userId = 0)
    {
        $picture = $input['picture'] ?? [];
        $picture = json_encode($picture);
        $this->user_id = $userId;
        $this->picture = $picture;
        $this->content = $input['content'] ?? '';

        $this->save();

        return ['code' => 0, 'msg' => '提交成功'];
    }

}
